<?php
class Favorable {

	static private $mInstance = null;

	static public function & Instance() {
		if (null == self :: $mInstance) {
			$class = __CLASS__;
			self :: $mInstance = new $class;
		}
		return self :: $mInstance;
	}

	function __construct() {

	}

	static public function clusterFavorAdd($x) {
		self :: Instance();
		$addnum = (int) $x;
		$date_time = date("Y-m-d H:i:s", time() + 8 * 3600);
		@ DB :: Query("update td_cluster_favorable set `favorable`=favorable+{$addnum},`update_time`='{$date_time}'  where ClusterNum='{$_POST[ClusterNum]}'");
		return;
	}

	static function clusterFavorDeduct($x) { //好感度惩罚
		self :: Instance();
		$addnum = (int) $x;
		@ DB :: Query("update td_cluster_favorable set `deduct`=deduct+{$addnum} where ClusterNum='{$_POST[ClusterNum]}'");
		return;
	}
	static function clusterFavReward($x) { //好感度奖励
		self :: Instance();
		$addnum = (int) $x;
		@ DB :: Query("update td_cluster_favorable set `reward`=reward+{$addnum} where ClusterNum='{$_POST[ClusterNum]}'");
		return;
	}

	static function detestFavor() { //根据内容判断是否惩罚好感度
		self :: Instance();
		$detest_word_arr = require (ROOT_PATH . 'Common/detestword.php');
		foreach ($detest_word_arr as $v) {
			if (@ strpos("yaya" . $_POST[Message], $v)) {
				self :: clusterFavorDeduct(2);
				return;
			}
		}
		return;
	}

	static function getClusterFav() {
		$cluster = $_POST[ClusterNum];
		$date_time = date("Y-m-d H:i:s", time() + 8 * 3600);
		$ret = DB :: LimitQuery('td_cluster_favorable', array (
			'condition' => array (
				'ClusterNum' => $cluster
			)
		), false);
		
		$enter_time = date("YmdHis", time() + 8 * 3600);
		if (count($ret) == 0) {
			$arr = array (
				'ClusterNum' => $cluster,
				'favorable' => 1,
				'update_time'=>$date_time,
				'enter_time' => $enter_time,
				'is_in_cluster'=>1,
				'ClusterName' => $_POST[ClusterName]
			);
			@ DB :: SaveTableRow('td_cluster_favorable', $arr);
			return 1;
		} else {
			@ DB :: Query("update td_cluster_favorable set `favorable`=favorable+1,`update_time`='{$date_time}' , `is_in_cluster`=1 ,`ClusterName`='{$_POST[ClusterName]}' where ClusterNum='{$_POST[ClusterNum]}'");
			return $ret[0][favorable];
		}
	}

	static function getFavLev($cluster) { //返回好感度信息
		self :: Instance();
		$ret = DB :: LimitQuery('td_cluster_favorable', array (
			'condition' => array (
				'ClusterNum' => $cluster
			)
		), false);
		$favorable = 0;
		if (count($ret) == 0) {
			$arr = array (
				'ClusterNum' => $cluster,
				'favorable' => 1
			);
			@ DB :: SaveTableRow('td_cluster_favorable', $arr);
			$favorable = 0;
		} else {
			$favorable = floor(($ret[0][favorable] + $ret[0][reward] - $ret[0][deduct]) - ($ret[0][favorable] + $ret[0][reward] - $ret[0][deduct]) * (($ret[0][favorable] - $ret[0][reward]) / ($ret[0][favorable] + $ret[0][reward])) - ($ret[0][favorable] + $ret[0][deduct]) * $ret[0][deduct] / $ret[0][favorable]);
		}

		$fav_arr = array (
			"讨厌的群",
			"无存在感的群",
			"陌生的群",
			"有趣的群",
			"好玩的群",
			"熟悉的群",
			"有爱的群",
			"非常有爱的群",
			"充满爱的群",
			"很喜欢的群",
			"非常喜欢的群",
			"最喜欢的群"
		);

		$lv = 0;
		if ($favorable <= -201) {
			$lv = -2;
		}
		elseif ($favorable <= -1 && $favorable >= 200) {
			$lv = -1;
		}
		elseif ($favorable <= 100 && $favorable >= 0) {
			$lv = 0;
		}
		elseif ($favorable <= 500 && $favorable >= 101) {
			$lv = 1;
		}
		elseif ($favorable <= 2000 && $favorable >= 501) {
			$lv = 2;
		}
		elseif ($favorable <= 5000 && $favorable >= 2001) {
			$lv = 3;
		}
		elseif ($favorable <= 13000 && $favorable >= 5001) {
			$lv = 4;
		}
		elseif ($favorable <= 30000 && $favorable >= 13001) {
			$lv = 5;
		}
		elseif ($favorable <= 50000 && $favorable >= 30001) {
			$lv = 6;
		}
		elseif ($favorable >= 50001 && $favorable <= 100000) {
			$lv = 7;
		}
		elseif ($favorable >= 100001 && $favorable <= 200000) {
			$lv = 8;
		}
		elseif ($favorable >= 200001) {
			$lv = 9;
		}

		return array (
			"lv" => $lv,
			"mess" => $fav_arr[$lv +2],
			"favorable" => $favorable
		);
	}

	static function viewFav() {
		self :: Instance();
		$arr = self :: getFavLev($_POST[ClusterNum]);
		$auth = new Authority();
		$lv = $arr[lv];
		$mess = "";
		switch ($lv) {
			case -2 :
				$mess = "索妮娅酱已经在这里无法继续呆下去了，请把索妮娅放出去吧，非常感谢~！";
				break;
			case -1 :
				$mess = "索妮娅酱在这里一点存在感都没- -，请问吧索妮娅抓进来到底是要干啥??";
				break;
			case 0 :
				$mess = "好陌生的地方哦，希望大家多多聊天，彼此增进感情呢~!";
				break;
			case 1 :
				$mess = "这个群挺有趣的呢。。索妮娅酱喜欢、、";
				break;
			case 2 :
				$mess = "哇，好好玩的群哦，大家一起来玩吧。。。喵";
				break;
			case 3 :
				$mess = "索妮娅酱天天来这儿呢。。大家都认识我吧。。";
				break;
			case 4 :
				$mess = "这个群好有爱哦，索妮娅酱一定会更加努力卖萌的>_<..";
				break;
			case 5 :
				$mess = "这里太有爱了，请大家一起，努力调教索妮娅酱吧>_<..";
				break;
			case 6 :
				$mess = "索妮娅酱感受到了，这里到处充满了爱。。>_<..";
				break;
			case 7 :
				$mess = "索妮娅酱很喜欢这里，大家一起来躲喵喵吧..";
				break;
			case 8 :
				$mess = "索妮娅酱非常喜欢这里，能跟大家一起玩，索妮娅酱真的很高兴>_<..";
				break;
			case 9 :
				$mess = "索妮娅酱最喜欢的地方就是这里啦、、、";
				break;

		}
		if($arr[lv]==9)
			$arr[lv] = "⑨";
		return "当前群『{$_POST[ClusterName]}』 好感度等级 Lv{$arr[lv]} " . ($auth->adminLevel > 0 ? "(" . $arr[favorable] . ")" : "") . " {$arr[mess]}\n" . $mess; //({$arr[favorable]}

	}
	
	static function getFavLevByFv() {
		
		$a = self::getFavLev($_POST[ClusterNum]);
		return $a['lv'];
		
	}
	
	
	public static function addUserInfo() {
		$ret = DB :: LimitQuery('td_normal_user_info', array (
			'condition' => array (
				'qq' => $_POST[Sender]
			)
		), false);
		if(count($ret)<=0) {
			$update_time = date("Y-m-d H:i:s", time() + 8 * 3600);
			$arr = array(
				'nick'=>$_POST[Nick],
				'qq'=>$_POST[Sender],
				'is_friend'=> 0,
				'is_blacklist'=>0,
				'favorable'=>1,
				'Invite_time'=>2,
				'update_time'=>$update_time
			);
			
			@DB :: SaveTableRow('td_normal_user_info', $arr);
		}
		
		
	}
	
	public static function updateUserInfo($i) {
		$update_time = date("Y-m-d H:i:s", time() + 8 * 3600);
		$fsql = ",`favorable`=favorable+{$i}" ;
		
		@ DB :: Query("update td_normal_user_info set `update_time`='{$update_time}'  {$fsql} ,`nick`='{$_POST[Nick]}'  where qq={$_POST[Sender]}  and is_blacklist=0");
		$_SESSION['is_update'] = $_SESSION['is_update'] +1 ;
	}
	
	public static function updateFriendFlag() {
		@ DB :: Query("update td_normal_user_info set `is_friend`=1 where qq={$_POST[Sender]}  and is_blacklist=0");
	}
	
	public static function getNomFavLevel($x) {
		$lv = 0;
		if($x>0 && $x<=100) {
			$lv = 1;
		} else if($x>101 && $x <=500){
			$lv =2;
		}else if($x>501 && $x <=1000){
			$lv =3;
		}else if($x>1001 && $x <=2000){
			$lv =4;
		}else if($x>2001 && $x <=5000){
			$lv =5;
		}else if($x>5001 && $x <=10000){
			$lv =6;
		}else if($x>10001 && $x <=20000){
			$lv =7;
		}
	}
	
	public static function getMyFavLevel() {
		$ret = DB :: LimitQuery('td_normal_user_info', array (
			'condition' => array (
				'qq' =>$_POST[Sender]
			)
		), false);
		$x= $ret[0][favorable];
		$lv = 0;
		return self::getNomFavLevel($x);
	}
}
?>